package code.structure.manager.bitmap
{
	
	import flash.display.MovieClip;
	import flash.utils.Dictionary;
	import code.structure.manager.loader.DllLoaderManager;

	/**
	 *素材转位图管理器 
	 * @author Yuri
	 * 
	 */
	public class MaterialBitmapCoverManager
	{
		private static var _instance:MaterialBitmapCoverManager;
		
		/**
		 *回调 
		 */
		private var _callBackDic:Dictionary;
		/**
		 *位图序列化集合  
		 */
		private var _bitmapListDic:Dictionary;
		
		/**
		 *是否需要排队转换
		 */
		private var _isNeedSortConvert:Boolean = false;
		/**
		 *排队队列 
		 */
		private var _sortList:Array;
		/**
		 *是否在排队中 
		 */
		private var _isSorting:Boolean;
		public function MaterialBitmapCoverManager()
		{
			if(_instance){ 
				throw new Error("实例化单例类出错-MaterialManager");
			}else{
				init();
			}
		}
		
		
		private function init():void{
			_callBackDic = new Dictionary();
			_bitmapListDic = new Dictionary();
			_sortList = [];
		}
		
		public static function getInstance():MaterialBitmapCoverManager{
			if(!_instance) _instance = new MaterialBitmapCoverManager();
			return _instance;
		}
		
		/**
		 *获取素材信息 
		 * @param key swf加载名称 xml中可见
		 * @param subKey
		 * @param bLoaderName
		 * @return 
		 * 
		 */
		public function getMaterial(key:*,subKey:String,callBackFun:Function=null,type:String = "swf"):*{
			var bKey:String ;
			var isHadInstance:Boolean;
			var bitMapDataList:Array;
			if(key is MovieClip){
				bKey = subKey;
				isHadInstance = true;
			}else if(key is String){
				bKey = key + "&" +subKey;
			}else{
				return;
			}
			if(_bitmapListDic[bKey]){
				if(callBackFun!=null) callBackFun(_bitmapListDic[bKey].concat());
			}else{
				pushCallBackfun(bKey,callBackFun);
				if(isHadInstance){
					bitMapDataList = YDisplayConvert.drawMoveClip(key as MovieClip);
					onCovertCompleteFun(bKey,bitMapDataList);
				}else{
					DllLoaderManager.getInstance().getMaterial(key,subKey,onDllgetMaterialHandler);
				}
			}
		}
		
		/**
		 *添加侦听回调函数 
		 * @param bKey
		 * @param callBackFun
		 * 
		 */
		private function pushCallBackfun(bKey:String,callBackFun:Function):void{
			if(callBackFun!=null){
				if(_callBackDic[bKey]) {
					_callBackDic[bKey].push(callBackFun);
					return;
				}else{
					_callBackDic[bKey] = [];
					_callBackDic[bKey].push(callBackFun);
				}
			}
		}
		
		/**
		 *检测排序转换素材 
		 * 
		 */
		private function checkSortConverMaterial():void{
			if(_isNeedSortConvert && !_isSorting && _sortList.length>0){
				var arr:Array = _sortList[0].concat();
				_isSorting = true;
				_sortList.shift();
				YDisplayConvert.convert(arr[0],arr[1],onCovertCompleteFun);
			}
		}
		
		/**
		 *获取到素材 
		 * @param c
		 * 
		 */
		private function onDllgetMaterialHandler(c:Class,bkey:String):void
		{
			if(_isNeedSortConvert){
				_sortList.push([bkey,c]);
				checkSortConverMaterial();
			}else{
				YDisplayConvert.convert(bkey,c,onCovertCompleteFun);
			}
		}
		
		
		
		/**
		 *转换成位图回调 
		 * @param subKey
		 * @param bitmapArr
		 * 
		 */
		private function onCovertCompleteFun(bkey:String,bitmapArr:Array):void
		{
			_bitmapListDic[bkey] = bitmapArr;
			if(_callBackDic[bkey]){
				for each(var fun:Function in _callBackDic[bkey]){
					fun(bitmapArr.concat());
				}
				delete _callBackDic[bkey];
			}
			if(_isNeedSortConvert){
				_isSorting = false;
				checkSortConverMaterial();
			}
		}
	}
}